package sz.szsmk.citizencard.mvp.presenter;

import com.google.gson.Gson;

import javax.inject.Inject;

import sz.szsmk.citizencard.model.network.GsdxNetApi;
import sz.szsmk.citizencard.model.network.bean.request.HotReq;
import sz.szsmk.citizencard.model.network.bean.request.RecommendReq;
import sz.szsmk.citizencard.model.network.bean.response.HotRes;
import sz.szsmk.citizencard.model.network.bean.response.RecommendRes;
import sz.szsmk.citizencard.mvp.contract.ISearchHotContact;
import sz.szsmk.citizencard.retrofitrxjava.ErrorAction;
import sz.szsmk.citizencard.retrofitrxjava.NetworkDateSources;
import sz.szsmk.citizencard.retrofitrxjava.SubscribeResult;

/**
 * Copyright (c)2019
 * author: zdh
 * created on: 2019/1/16 5:00
 * description:
 */
public class SearchHotPresenter extends IBasePresenter
        implements ISearchHotContact.ISearchHotPresenter<ISearchHotContact.ISearchHotView> {


    private NetworkDateSources mNetworkDateSource;
    GsdxNetApi gsdxNetApi;

    @Inject
    public SearchHotPresenter(GsdxNetApi gsdxNetApi) {
        this.gsdxNetApi = gsdxNetApi;

        this.mNetworkDateSource = NetworkDateSources.getInstance();
    }

    @Override
    public void getHot() {
        HotReq hotReq = new HotReq();
        hotReq.setPage_num(1);
        hotReq.setPage_size(20);
        String json = new Gson().toJson(hotReq);
        mNetworkDateSource.hot(hotReq, new SubscribeResult<HotRes>() {
            @Override
            protected void onOk(HotRes response) {
                ((ISearchHotContact.ISearchHotView) mView).getHotSucc(response);
            }

            @Override
            protected void onFailure(HotRes response) {
                super.onFailure(response);
                ((ISearchHotContact.ISearchHotView) mView).getHotFail(response);
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });

    }

    @Override
    public void getRecom(String key) {
        RecommendReq recommendReq = new RecommendReq();
        recommendReq.setKey_word(key);
        recommendReq.setPage_num(1);
        recommendReq.setPage_size(20);
        String json = new Gson().toJson(recommendReq);
        mNetworkDateSource.recommend(recommendReq, new SubscribeResult<RecommendRes>() {
            @Override
            protected void onOk(RecommendRes response) {
                ((ISearchHotContact.ISearchHotView) mView).getRecomSucc(response);
            }

            @Override
            protected void onFailure(RecommendRes response) {
                super.onFailure(response);
                ((ISearchHotContact.ISearchHotView) mView).getRecomFail(response);
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });
    }

}
